/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "MusicChangerAndroid.h"
#include "Manager.h"
#include <logs/Log.h>
#include <math/utl.h>
#include <jni.h>
#include "JavaMethodCacheMusic.h"


namespace liba
{
namespace noise
{

MusicChanger::MusicChanger(double fadeout_time)
:	global_volume( 1 ),
	current_volume( 1 ),
	fadeout_speed( 1.0 / fadeout_time )
{
	logs::log() << "music changer create";
//	check_method_cache_music();
}

MusicChanger::~MusicChanger()
{
return;

	if(method_cache_music->main_object)
		gJavaEnv->DeleteGlobalRef(method_cache_music->main_object);
		
	if(method_cache_music->main_class)
		gJavaEnv->DeleteGlobalRef(method_cache_music->main_class);
		
	delete method_cache_music; method_cache_music = 0;
}

void MusicChanger::change_music(const filesystem::String & next_music)
{
return;

	next_music_name = next_music;
}

void MusicChanger::life_cycle(double delta_time)
{
return;

	if( next_music_name == music_name )
	{
		current_volume += delta_time * fadeout_speed;
		current_volume = math::clamp( current_volume, 0.0, 1.0 );
		gJavaEnv->CallVoidMethod(method_cache_music->main_object, method_cache_music->set_volume, current_volume * global_volume);
	}
	else
	{
		current_volume -= delta_time * fadeout_speed;
		gJavaEnv->CallVoidMethod(method_cache_music->main_object, method_cache_music->set_volume, current_volume * global_volume);

		if( current_volume < 0 )// || channel->is_finished() )
		{
			music_name = next_music_name;
			if( !music_name.empty() )
			{
				current_volume = 1;
				gJavaEnv->CallVoidMethod(method_cache_music->main_object, method_cache_music->set_volume, current_volume * global_volume);
				
				jstring nameJStr = gJavaEnv->NewStringUTF(music_name.c_str());
				if (nameJStr != NULL) {
					gJavaEnv->CallVoidMethod(method_cache_music->main_object, method_cache_music->play_music, nameJStr);
				}
			}
		}
	}
}

void MusicChanger::change_volume(double volume)
{
return;

	global_volume = volume;
}

} // namespace noise
} // namesapce liba
